package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlanVo> list = nursingPlanMapper.getAll();
        return list;
    }

    @Override
    public void add(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(nursingPlanDto,nursingPlan);
        nursingPlan.setStatus(0);
        List<NursingProjectPlanVo> npps = nursingPlan.getProjectPlans();
        nursingPlanMapper.add(nursingPlan);
        Long id = nursingPlan.getId();
        for (NursingProjectPlanVo npp : npps) {
            npp.setPlanId(id);
            nursingPlanMapper.addMiddle(npp);
        }
    }

    @Override
    public PageResponse<NursingPlanVo> page(String name, Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage(pageNum,pageSize);
        List<NursingPlanVo> list = nursingPlanMapper.page(name,status);
        Page<NursingPlanVo> page = (Page<NursingPlanVo>) list;
        return PageResponse.of(page,NursingPlanVo.class);
    }

    @Override
    public NursingPlanVo getById(Integer id) {
        NursingPlan np = nursingPlanMapper.getById(id);
        List<NursingProjectPlanVo> list = nursingPlanMapper.getList(id);
        NursingPlanVo nv = new NursingPlanVo();
        BeanUtil.copyProperties(np,nv);
        nv.setProjectPlans(list);
        return nv;
    }

    @Override
    public void update(NursingPlanDto nursingPlanDto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtil.copyProperties(nursingPlanDto,nursingPlan);
        List<NursingProjectPlanVo> nppVo = nursingPlan.getProjectPlans();
        for (NursingProjectPlanVo nursingProjectPlanVo : nppVo) {
            nursingPlanMapper.updateMiddle(nursingProjectPlanVo);
            // Long projectId = nursingProjectPlanVo.getProjectId();
            // nursingProjectPlanVo.setPlanId(id);
            // nursingPlanMapper.deleteMiddle(projectId);
            // nursingPlanMapper.insertMiddle(nursingProjectPlanVo);
        }
        nursingPlanMapper.update(nursingPlan);
    }

    @Override
    public void deleteById(Integer id) {
        nursingPlanMapper.deleteById(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        nursingPlanMapper.updateStatus(id,status);
    }
}
